﻿using Microsoft.AspNetCore.Mvc;
using Util.Model;
using Util;

namespace QuickCode.WebApi.Controllers
{
    /// <summary>
    /// 返回数据打包器
    /// </summary>
    [ApiController]
    public class BaseController : Controller
    {
        /// <summary>
        /// 返回空或对象
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public WebApiPackage OK(object obj = null)
        {
            return new WebApiPackage
            {
                success = true,
                resultCode = 200,
                resultDesc = "调用成功",
                result = obj,
            };
        }

        /// <summary>
        /// 返回数据
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public WebApiPackage<T> Data<T>(T result)
        {
            return new WebApiPackage<T>
            {
                success = true,
                resultCode = 200,
                resultDesc = "调用成功",
                result = result,
            };
        }

        /// <summary>
        /// 返回列表
        /// </summary>
        /// <returns></returns>
        [NonAction]
        public WebApiPackageList<T> List<T>(IEnumerable<T> list, Pagination pagination)
        {
            return new WebApiPackageList<T>
            {
                success = true,
                resultCode = 200,
                resultDesc = "调用成功",
                result = new WebApiPackageList1<T>
                {
                    list = list,
                    pager = pagination,
                }
            };
        }
        /// <summary>
        /// 返回列表和其他数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="pagination"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [NonAction]
        public WebApiPackageList<T> List<T>(IEnumerable<T> list, Pagination pagination, object data)
        {
            return new WebApiPackageList<T>
            {
                success = true,
                resultCode = 200,
                resultDesc = "调用成功",
                result = new WebApiPackageList1<T>
                {
                    list = list,
                    pager = pagination,
                    data = data
                }
            };
        }
    }
}
